package com.bsj.media.network;

import com.bsj.tools.Conv;
import lombok.Data;

/**
 * RTP音频组包
 */
@Data
public class RTPAudioData {

    private int pt; // 负载类型
    private byte[] simId; // 终端号
    private int ch; // 通道号
    private int dateType; // 数据类型 0000视频I帧，0001视频P帧，0010视频B帧，0011音频帧，

    private short packetIndex; // 包序列号
    private long timestamp; // 时间戳

    public RTPAudioData() {
        super();
        this.ch = 36;
        this.dateType = 3;
        this.pt = 6;
    }

    public RTPAudioData(int pt, byte[] simId, int ch, int dateType) {
        super();
        this.pt = pt;
        this.simId = simId;
        this.ch = ch;
        this.dateType = dateType;
    }

    //打包成发给设备的RTP数据
    public byte[] packetAudioRTP(byte[] body, int bodyLen) {
        int len = body.length + 30;
        byte[] rtpData = new byte[len];
        int offset = 0;
        // RTP 帧头4字节
        rtpData[offset++] = 0x30;
        rtpData[offset++] = 0x31;
        rtpData[offset++] = 0x63;
        rtpData[offset++] = 0x64;
        //固定
        rtpData[offset++] = (byte) 0x81;
        //负载类型
        rtpData[offset++] = ((byte) (this.pt | 0x80));
        //包序号
        Conv.setShortNetOrder(rtpData, offset, packetIndex);
        packetIndex++;
        offset = offset + 2;
        int simIdLen = simId.length;
        // SIM卡号，6位
        System.arraycopy(simId, 0, rtpData, offset, simIdLen);
        offset += simIdLen;
        //时间戳
        Conv.setLongNetOrder(rtpData, offset, timestamp);
        offset = offset + 8;
        // 音频每秒发送25帧，每帧间隔40ms
        timestamp = timestamp + 40;
        //数据体长度
        Conv.setShortNetOrder(rtpData, offset, bodyLen);
        offset = offset + 2;
        System.arraycopy(body, 0, rtpData, offset, bodyLen);
        offset = offset + bodyLen;
        return rtpData;
    }

}
